package com.hzt.system.controller;

import com.hzt.system.common.ActiveUser;
import com.hzt.system.common.DataGridView;
import com.hzt.system.common.upload.UploadProperties;
import com.hzt.system.common.upload.UploadService;
import com.hzt.system.domain.User;
import com.hzt.system.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("api/file")
public class FileController {

    @Autowired
    private UploadService uploadService;
    @Autowired
    private UploadProperties uploadProperties;
    @Autowired
    private UserService userService;

    //上传文件
    @RequestMapping("uploadFile")
    public Object uploadile(MultipartFile mf){
        String path = this.uploadService.uploadImage(mf);
        String baseUrl = this.uploadProperties.getBaseUrl();
        Map<String,Object> map =new HashMap<>();
        map.put("src",path);
        //返回前更新数据库
        ActiveUser principal = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        User user = principal.getUser();
        user.setImgpath(path);
        this.userService.updateUser(user);
        return new DataGridView(map);
    }

    //上传文件
    @RequestMapping("uploadGoodsFile")
    public Object uploadGoodsFile(MultipartFile mf){
        String path = this.uploadService.uploadImage(mf);
        Map<String,String> map=new HashMap<>();
        map.put("src",path);
        return new DataGridView(map);
    }


}
